package com.example.gokchinesefoodmapmcdev.util;

import com.example.gokchinesefoodmapmcdev.entity.goods.GoodsInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class MyTranslateTools {
    private static final Logger logger = LoggerFactory.getLogger(MyTranslateTools.class);

    /**
     * 利用反射机制，将实体类中的属性名和属性值作为key-value封装在map集合中
     * @param entity  需要转换的实体对象
     * @return
     */
    public static Map<String, Object> transEntityToMap(Object entity) {
        Map<String, Object> map = new HashMap<>();
        Class<?> clazz = entity.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            try {
                map.put(field.getName(), field.get(entity));
            } catch (IllegalAccessException e) {
                logger.error("=====================自定义工具类MyTranslateTools转换失败异常记录=====================");
                logger.error(e.getMessage());
            }
        }
        return map;
    }

    /**
     * 商品的状态值都是数字，将其转为中文意思
     *      因为有多个地方需要用到，在这里同一个封装调用
     */
    public static void addStatusZn(Map<String, Object> map, GoodsInfo goodsInfo){
        // 1. 上架状态
        String onSaleZn = null;
        if(goodsInfo.getOnSale() != null) {
            if (Integer.parseInt(goodsInfo.getOnSale()) == 0) {
                onSaleZn = "否";
            } else if (Integer.parseInt(goodsInfo.getOnSale()) == 1) {
                onSaleZn = "是";
            }
        }
        map.put("onSaleZn", onSaleZn);
        // 2. 商品分类（数据库表中没有分类信息，暂设置为空）
        map.put("goodTypeZn", null);
        // 3. 审核状态
        String reviewStatusZn = null;
        if(goodsInfo.getReviewStatus() != null) {
            if (Integer.parseInt(goodsInfo.getReviewStatus()) == 0) {
                reviewStatusZn = "待审核";
            } else if (Integer.parseInt(goodsInfo.getReviewStatus()) == 1) {
                reviewStatusZn = "已通过";
            } else if (Integer.parseInt(goodsInfo.getReviewStatus()) == -1) {
                reviewStatusZn = "已拒绝";
            }
        }
        map.put("reviewStatusZn", reviewStatusZn);
    }



}
